package com.example.service.impl;

import com.example.dao.TbItemCatMapper;
import com.example.dao.TbItemDescMapper;
import com.example.dao.TbItemMapper;
import com.example.dao.TbItemParamItemMapper;
import com.example.dto.EasyUIDataGridResult;
import com.example.dto.EasyUITreeNode;
import com.example.dto.Result;
import com.example.entity.TbItem;
import com.example.entity.TbItemCat;
import com.example.entity.TbItemDesc;
import com.example.entity.TbItemParamItem;
import com.example.service.ItemService;
import com.example.util.IDUtils;
import com.example.util.JsonUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p></p>
 * Created by zhezhiyong@163.com on 2018/7/15.
 */
@Service("itemService")
public class ItemServiceImpl implements ItemService {

    @Resource
    private TbItemMapper tbItemMapper;
    @Resource
    private TbItemCatMapper tbItemCatMapper;
    @Resource
    private TbItemDescMapper tbItemDescMapper;
    @Resource
    private TbItemParamItemMapper tbItemParamItemMapper;

    @Override
    public EasyUIDataGridResult getItemList(int page, int rows) {
        PageHelper.startPage(page, rows);
        Example example = new Example(TbItem.class);
        List<TbItem> tbItemList = tbItemMapper.selectByExample(example);
        EasyUIDataGridResult result = new EasyUIDataGridResult();
        PageInfo<TbItem> pageInfo = new PageInfo<>(tbItemList);
        result.setTotal(pageInfo.getTotal());
        result.setRows(tbItemList);
        return result;
    }

    @Override
    public List<EasyUITreeNode> getItemCatList(long parentId) {
        // 根据parentId查询分类列表
        Example example = new Example(TbItemCat.class);
        //设置查询条件
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("parentId", parentId);
        //执行查询
        List<TbItemCat> list = tbItemCatMapper.selectByExample(example);
        //转换成EasyUITreeNode列表
        List<EasyUITreeNode> resultList = new ArrayList<>();
        for (TbItemCat tbItemCat : list) {
            //创建一个节点对象
            EasyUITreeNode node = new EasyUITreeNode();
            node.setId(tbItemCat.getId());
            node.setText(tbItemCat.getName());
            node.setState(tbItemCat.getIsParent() ? "closed" : "open");
            //添加到列表中
            resultList.add(node);
        }
        return resultList;
    }

    @Override
    public Result saveItem(TbItem tbItem, String desc, String itemParams) {
        long itemId = IDUtils.genItemId();
        tbItem.setId(itemId);
        // 商品状态 1-正常，2-下架，3-删除
        tbItem.setStatus((byte) 1);
        Date date = new Date();
        tbItem.setCreated(date);
        tbItem.setUpdated(date);
        tbItemMapper.insert(tbItem);
        TbItemDesc itemDesc = new TbItemDesc();
        itemDesc.setItemId(itemId);
        itemDesc.setItemDesc(desc);
        itemDesc.setCreated(date);
        itemDesc.setUpdated(date);
        tbItemDescMapper.insert(itemDesc);
        // 商品规格
        TbItemParamItem itemParamItem = new TbItemParamItem();
        itemParamItem.setParamData(itemParams);
        itemParamItem.setItemId(itemId);
        itemParamItem.setCreated(new Date());
        itemParamItem.setUpdated(new Date());
        tbItemParamItemMapper.insert(itemParamItem);
        return Result.ok();
    }

    @Override
    public String getItemParamHtml(Long itemId) {
        Example example = new Example(TbItemParamItem.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("itemId", itemId);
        List<TbItemParamItem> itemParamItemList = tbItemParamItemMapper.selectByExample(example);
        if (itemParamItemList == null||itemParamItemList.isEmpty()){
            return "";
        }
        TbItemParamItem itemParamItem = itemParamItemList.get(0);
        String paramData = itemParamItem.getParamData();
        List<Map> mapList = JsonUtils.jsonToList(paramData, Map.class);
        StringBuilder sb = new StringBuilder();
        sb.append("<div class=\"Ptable\">\n");
        sb.append("    <div class=\"Ptable-item\">\n" );
        for (Map map : mapList) {
            sb.append("        <h3>"+map.get("group")+"</h3>\n" );
            sb.append("        <dl>\n" );
            List<Map> mapList1 = (List<Map>) map.get("params");
            for (Map map1 : mapList1) {
                sb.append("            <dt>"+map1.get("k")+"</dt><dd>"+map1.get("v")+"</dd>\n" );
            }
            sb.append("        </dl>\n" );
        }
        sb.append("    </div>\n" );
        sb.append("</div>");
        return sb.toString();
    }
}
